package com.hsytech.dijiebao.home.fragment;

import java.util.ArrayList;
import java.util.List;

import com.hsytech.dijiebao.R;
import com.hsytech.dijiebao.bean.HomeHotelBean;
import com.hsytech.dijiebao.home.Frament00;
import com.hsytech.dijiebao.home.activity.HomeDetailActivity;
import com.hsytech.dijiebao.home.activity.MoreActivity;
import com.hsytech.dijiebao.home.alldetail.HotelDetailActivity;
import com.hsytech.dijiebao.home.alldetail.MyselfHotelDetailActivity;
import com.hsytech.dijiebao.utils.Constants;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class HotleFrament extends Fragment implements OnClickListener {

    public void setData(List<HomeHotelBean> myHotelList, List<HomeHotelBean> zuHotelList) {
        this.myHotelList = myHotelList;
        this.zuHotelList = zuHotelList;
    }

    private View root;
    private List<HomeHotelBean> myHotelList = new ArrayList<>();
    private List<HomeHotelBean> zuHotelList = new ArrayList<>();

    private LinearLayout qiu_zu_hotel_title, my_hotel_title;
    private LayoutInflater inflater;
    private Adapter zuAdapter;
    private Adapter myAdapter;
    private ListView zu_hotel_listView, my_hotel_listView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_home_hotel, container, false);
        this.inflater = LayoutInflater.from(getActivity());
        zu_hotel_listView = (ListView) root.findViewById(R.id.zu_hotel_listView);
        my_hotel_listView = (ListView) root.findViewById(R.id.my_hotel_listView);
        qiu_zu_hotel_title = (LinearLayout) root.findViewById(R.id.qiu_zu_hotel_title);
        my_hotel_title = (LinearLayout) root.findViewById(R.id.my_hotel_title);

        qiu_zu_hotel_title.setOnClickListener(this);
        my_hotel_title.setOnClickListener(this);

        zuAdapter = new Adapter(zuHotelList);
        myAdapter = new Adapter(myHotelList);

        zu_hotel_listView.setAdapter(zuAdapter);
        my_hotel_listView.setAdapter(myAdapter);

        zu_hotel_listView.setOnItemClickListener(new MyListOnItemClick(zuHotelList));
        my_hotel_listView.setOnItemClickListener(new MyListOnItemClick(myHotelList));

        return root;
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.qiu_zu_hotel_title) {
            Intent intent = new Intent(getActivity(), MoreActivity.class);
            intent.putExtra("title", "求租酒店");
            intent.putExtra("type", "1");
            intent.putExtra("url", Constants.HTTP_MORE_HOTLE);
            startActivity(intent);

        } else if (v.getId() == R.id.my_hotel_title) {
            Intent intent = new Intent(getActivity(), MoreActivity.class);
            intent.putExtra("title", "我有酒店");
            intent.putExtra("type", "2");
            intent.putExtra("url", Constants.HTTP_MORE_MY_HOTLE);
            startActivity(intent);

        }
    }

    private class MyListOnItemClick implements ListView.OnItemClickListener {

        private List<HomeHotelBean> list;

        public MyListOnItemClick(List<HomeHotelBean> list) {
            this.list = list;
        }


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent();
            intent.setClass(getActivity(), HomeDetailActivity.class);
            intent.putExtra("id", list.get(position).getId());
            intent.putExtra("type", "1");
            startActivity(intent);

        }
    }


    private class Adapter extends BaseAdapter {

        private List<HomeHotelBean> list;

        public Adapter(List<HomeHotelBean> list) {
            this.list = list;
        }


        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = inflater.inflate(R.layout.item_home_list, null);
            TextView city_name = (TextView) convertView.findViewById(R.id.city_name);
            TextView content = (TextView) convertView.findViewById(R.id.content);
            TextView publisher_name = (TextView) convertView.findViewById(R.id.publisher_name);
            ImageView hot = (ImageView) convertView.findViewById(R.id.hot);
            ImageView recommend = (ImageView) convertView.findViewById(R.id.recommend);
            ImageView news = (ImageView) convertView.findViewById(R.id.news);

            city_name.setText(list.get(position).getCity_name());
            city_name.setTextColor(Color.parseColor(list.get(position).getCity_color().replace("0x", "#")));
            content.setText(list.get(position).getHotel_cat_name() + " " + list.get(position).getStart_time() + "-"
                    + list.get(position).getEnd_time() + " " + getString(R.string.base_chen_che) + list.get(position).getCheck_in_num() + getString(R.string.base_ren));
            publisher_name.setText(getString(R.string.base_fa_bu_ren) + " " + list.get(position).getPublisher_name());

            if (list.get(position).getIs_hot().equals("1")) {
                hot.setVisibility(View.VISIBLE);
            } else {
                hot.setVisibility(View.GONE);
            }
            if (list.get(position).getIs_new().equals("1")) {
                news.setVisibility(View.VISIBLE);
            } else {
                news.setVisibility(View.GONE);
            }
            if (list.get(position).getIs_recommend().equals("1")) {
                recommend.setVisibility(View.VISIBLE);
            } else {
                recommend.setVisibility(View.GONE);
            }

            return convertView;
        }
    }
}
